import json
from flask import request, render_template, make_response, jsonify
from flask_restx import Namespace, Resource, fields
from flask_restx.swagger import DEFAULT_RESPONSE_DESCRIPTION

import server.apis.task.api
import celery_app.tasks

ns = Namespace('task', description='task')


@ns.route('', strict_slashes=False)
class TaskHandler(Resource):

    def get(self):
        try:
            task_id = request.args.get('task_id')
        except:
            return make_response(jsonify(message='参数错误'), 401)

        data = server.apis.task.api.get(task_id)
        res = {
            "task_id": data['task_id'],
            "status": data['status']
        }
        result = {} if res["status"] != 1 else {
            "name": "tom",
            "age": 88
        }
        res["result"] = result

        return jsonify(res)


    def post(self):
        try:
            params = json.loads(request.get_data(as_text=True))
            name = params.get('name')
            age = params.get('age')
        except:
            return make_response(jsonify(message='参数错误'), 401)

        celery_app.tasks.test.delay()
        return jsonify(message='success')
